<?php

namespace App\Http\Controllers\Api\v1;

use App\Handlers\KeysHandler;
use App\Http\Requests\Api\v1\WithdrawalRequest;
use App\Http\Resources\OrderResource;
use App\Models\Account;
use App\Models\Card;
use App\Models\GoodsOrder;
use App\Models\Order;
use App\Models\User;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\JsonResponse;
use Carbon\Carbon;
use App\Models\Configuration;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;

class WithdrawalController extends Controller
{

    /**
     * 提现列表
     * @param WithdrawalRequest $request
     * @param Order $order
     * @return JsonResponse|object
     */
    public function index(WithdrawalRequest $request, Order $order)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 10);
        $date = $request->get('date', '');
        if (empty($date)) {
            $date = '';
        }
        $recharge = $order->list($request->user(), Order::WITHDRAWAL, $this->paging($page, $limit), $limit, $date);
        return $this->success(OrderResource::collection($recharge));

    }


    /**
     * 提现
     * @param WithdrawalRequest $request
     * @param Order $order_model
     * @return JsonResponse|object
     */
    public function store(WithdrawalRequest $request, Order $order_model)
    {
        $user = User::getUser(['tiktokid'=>$request->header('Userid')]);
        if(!empty($user->payment_password)){
            if (!Hash::check($request->payment_password, $user->payment_password)) {
                return $this->fail(10007, trans('error.10007'));
            }
        }

        if($user->withdrawal_switch == 0){
            return $this->fail(10002, trans('error.10002'));
        }

        try {
            $amount = $request->get('amount');
            //余额
            if ($amount > $user->balance) {
                return $this->fail(10016, trans('error.10016'));
            }

            $min = Configuration::getItem('min_withdrawal_amount') * 100;
            $max = Configuration::getItem('max_withdrawal_amount') * 100;

            if ($amount < $min || $amount > $max) {
                return $this->fail(60005, sprintf(trans('error.60005'), $min/100, $max/100));
            }

            if(Configuration::getItem('order_limit_withdrawal_switch') === null || Configuration::getItem('order_limit_withdrawal_switch') == 1){
                //有待支付订单，不能提现
                $goods_orders = GoodsOrder::query()
                    ->where('user_id','=', $user->id)
                    ->where('add_time', '<',now())
                    ->where('order_status','=',GoodsOrder::ORDER_UNPAID)
                    ->exists();
                if($goods_orders){
                    return $this->fail(500001, trans('error.500001'));
                }
            }

            $card = Card::find($request->get('card_id'));
            $res = $order_model->createWithdrawOrder($user, $amount,$card);
            if ($res == 1) {
                return $this->success();
            }
            return $this->fail($res, trans('error.' . $res));
        } catch (Exception $exception) {
            return $this->fail(40003, trans('error.40003'));
        }
    }


}
